//
//  YQVisivtorView.swift
//  WeiBO
//
//  Created by a on 16/2/29.
//  Copyright © 2016年 xinji. All rights reserved.
//

import UIKit
import SnapKit

//定义协议 ->定义代理（weak）->设置代理->实现方法
protocol YQVisivtorViewdelegate :NSObjectProtocol{
    
    func didLogin()
    
    func didRegister()

}

//因为四个登陆界面差不多可以用该类来统一的表示出来
class YQVisivtorView: UIView {

    
    weak var delegate:YQVisivtorViewdelegate?
    //自定义视图的三个步奏
    //1.重写父类的构造方法
   override init(frame: CGRect) {
      super.init(frame: frame)
    
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
    
        setUpUI()
        
    }
    ///为了使通过xib创建的也能够通过解析出来
   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
    
    //2.添加设置UI的私有的方法
    private func setUpUI(){
        
        //添加控件
        addSubview(homeIconImageView)
        //添加遮罩视图
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        //添加约束
        //转动动画居中对齐
//        homeIconImageView.snp_makeConstraints { (make) -> Void in
//            make.center.equalTo(self.snp_center)
//        }
        homeIconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
        //
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
        //添加提示label -- 距离上边就是homeIconImageView
        // 距离中间
        // 宽的约束 216
        messageLabel.snp_makeConstraints { (make) -> Void in
            
            //距离动画视图的底部+ 10的间距
            make.top.equalTo(homeIconImageView.snp_bottom).offset(14)
            make.centerX.equalTo(self.snp_centerX)
            //
            make.width.equalTo(216)
        }
        
        //注册按钮
        /*
        距离左边是 -messageLabel的左边
        上边是 messageLabel的底部
        宽100 高30
        */
        
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messageLabel.snp_left)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            
            make.width.equalTo(100)
            make.height.equalTo(30)
            
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLabel.snp_right)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        
        //添加遮罩视图的约束
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
    }
    
    
///  添加动画效果
    private func startAnimation()
    {
       let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2*M_2_PI
        
        animation.repeatCount = MAXFLOAT
        
        animation.duration = 5.0
        
        animation.removedOnCompletion = false
        
        animation.fillMode = kCAFillModeForwards
        
        homeIconImageView.layer.addAnimation(animation, forKey: nil)
        
        
    }
    
    
    func setInfo(message:String,imageName:String,isanimation:Bool){
        
        messageLabel.text = message
        
        iconImageView.image = UIImage(named: imageName)
        
        //如果有动画我们就让他显示,并且执行
        if isanimation {
            
            homeIconImageView.hidden = false
            startAnimation()
            
        } else {
            homeIconImageView.hidden = true
            //可以移除
        }

    
    }
    
    //3.懒加载子空间
    //首页动画图片
    private lazy var homeIconImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        return img
        
    }()
    //通用的图片
    private lazy var iconImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_house")
        
        return img
        
    }()
    
    //提示语
    private lazy var messageLabel: UILabel = {
        
        let label = UILabel()
        
        // 设置属性
        label.font = UIFont.systemFontOfSize(12)
        
        label.textColor = UIColor.darkGrayColor()
        
        label.numberOfLines = 0
        
        label.textAlignment = .Center
        
        label.text = "关注一些人，回这里看看有什么惊喜"
        
        return label
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
        
        let button = UIButton(type: UIButtonType.Custom)
        
//        let StrImage = UIImage.stretchableImageWithLeftCapWidth()
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("注册", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return button
        
    }()
    
    //可选项的好处就可以发现
    func register(){

        delegate?.didRegister()
        
    }
    
    
    func login(){
        
        delegate?.didLogin()
    }
    
    //登陆
    private lazy var loginButton: UIButton = {
        
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("登陆", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        
    }()
    
    //遮罩视图
    private lazy var maskImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return img
    }()
    
}
